package com.kakame.codenews.base.web.listener;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kakame.codenews.base.config.system.SystemConfig;
import com.kakame.codenews.base.util.SpringUtil;
import com.kakame.codenews.base.web.fast.mapping.common.Menu;
import com.kakame.codenews.front.menu.service.FrontMenuService;
import com.kakame.codenews.front.menu.service.impl.FrontMenuServiceImpl;

/**
 * <pre>
 * <b>desc:</b> 项目启动时执行 
 * <b>auth:</b> 何家明
 * <b>date:</b> 2018年6月5日 下午2:29:26
 * </pre>
 */
@WebListener
public class StartUpListener implements ServletContextListener {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getPackage().getName());

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        servletContext.setAttribute("siteName", SystemConfig.get("site_name"));

        logger.info("菜单读取开始...");
        FrontMenuService menuService = SpringUtil.getBean(FrontMenuServiceImpl.class);
        List<Menu> menuList = menuService.listExistSiteHeaderMenu();
        servletContext.setAttribute("siteHeaderMenus", menuList);
        logger.info("菜单读取结束...");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }
}